﻿using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace ncp.Business.FrendLink
{
   public class FrendLinkBll
    {

        /// <summary>
        /// 得到新闻记录数
        /// </summary>
       public static int GetFrendLinkCount(string pcWhere, string pDataBase)
        {
            string sqlStr = "select count(*) from bi_FLink";
            if (!string.IsNullOrEmpty(pcWhere))
                sqlStr += " where " + pcWhere;
            return CommonBll.ReadInteger(sqlStr, pDataBase);
        }
        /// <summary>
        /// 得到新闻信息表
        /// </summary>
       public static DataTable GetFrendLinkTable(string fieldList, Dictionary<string, object> dicWhere, string order, string pDataBase)
        {
            List<SqlParameter> ilist = new List<SqlParameter>();
            string pcWhere = "1=1 ";
            foreach (KeyValuePair<string, object> kvp in dicWhere)
            {
                if (kvp.Key != "more")
                {
                    pcWhere += " and " + kvp.Key + "=@" + kvp.Key;
                    ilist.Add(new SqlParameter("@" + kvp.Key, kvp.Value));
                }
                else
                {
                    pcWhere += " and " + kvp.Value;
                }
            }
            string sqlStr = "select " + fieldList + " from bi_FLink where " + pcWhere+" "+order;
            SqlParameter[] paras = ilist.ToArray();
            return CommonBll.GetdtList(sqlStr, pDataBase, paras);
        }

       /// <summary>
       /// 得到分页新闻信息表
       /// </summary>
       public static DataTable GetFrendLinkTable(int piPageIndex, int piPageSize, string pcFieldList, string pcOrderBy, Dictionary<string, object> dicWhere, string pDataBase)
       {
           List<SqlParameter> ilist = new List<SqlParameter>();
           string pcWhere = "1=1 ";
           foreach (KeyValuePair<string, object> kvp in dicWhere)
           {
               pcWhere += " and " + kvp.Key + "=@" + kvp.Key;
               ilist.Add(new SqlParameter("@" + kvp.Key, kvp.Value));
           }
           string sqlStr = CommonBll.GetPagingSQL(piPageIndex, piPageSize, "bi_FLink", pcFieldList, "Link_Id", pcOrderBy, pcWhere);
           SqlParameter[] paras = ilist.ToArray();
           return CommonBll.GetdtList(sqlStr, pDataBase, paras);
       }

       /// <summary>
       /// 删除新闻
       /// </summary>
       public static int DeleteFrendLink(string Link_Id, string pDataBase)
       {
           string sqlStr = "delete from bi_FLink where Link_Id in(" + Link_Id + ")";
           return CommonBll.ExecuteNonQuery(sqlStr, pDataBase);
       }

       /// <summary>
       /// 修改
       /// </summary>
       public static int UpdateFrendLink(Dictionary<string, object> dicMessage, Dictionary<string, object> dicWhere, string pDataBase)
       {
           List<SqlParameter> ilist = new List<SqlParameter>();
           string field = "", where = "";
           if (dicMessage.Count == 0)
           {
               return 0;
           }
           foreach (KeyValuePair<string, object> kvp in dicMessage)
           {
               field += "," + kvp.Key + "=@" + kvp.Key;
               ilist.Add(new SqlParameter("@" + kvp.Key, kvp.Value));
           }
           foreach (KeyValuePair<string, object> kvp in dicWhere)
           {
               where += " and " + kvp.Key + "=@" + kvp.Key;
               ilist.Add(new SqlParameter("@" + kvp.Key, kvp.Value));
           }
           SqlParameter[] paras = ilist.ToArray();
           string sqlStr = "UPDATE dbo.bi_FLink set " + field.Substring(1) + " where 1=1 " + where;
           return CommonBll.ExecuteNonQuery(CommandType.Text, sqlStr, pDataBase, paras);
       }

       /// <summary>
       /// 增加留言信息
       /// </summary>
       public static int InsertFrendLink(Dictionary<string, object> dicMessage, string pDataBase)
       {
           List<SqlParameter> ilist = new List<SqlParameter>();
           string field = "", values = "";
           if (dicMessage.Count == 0)
           {
               return 0;
           }
           foreach (KeyValuePair<string, object> kvp in dicMessage)
           {
               field += "," + kvp.Key;
               values += ",@" + kvp.Key;
               ilist.Add(new SqlParameter("@" + kvp.Key, kvp.Value));
           }
           SqlParameter[] paras = ilist.ToArray();
           string sqlStr = "insert into dbo.bi_FLink (" + field.Substring(1) + ") values (" + values.Substring(1) + ")";
           return CommonBll.ExecuteNonQuery(CommandType.Text, sqlStr, pDataBase, paras);
       }
    }
}
